<?php
/**
 * @author: liaofei<136327134@qq.com>
 * @day: 2021/6/8
 */

namespace App\Dao\user;

use App\Dao\BaseDao;
use App\Models\system\SystemAdminModel;
use App\Models\user\UserInfoModel;
use App\Dao\system\SystemAdminDao;
use Illuminate\Support\Facades\DB;
use App\Exceptions\ApiException as HOException;
use App\Services\system\SystemLogServices;
/**
 * Class UserInfoDao
 * @package app\dao\user
 */
class UserInfoDao extends BaseDao
{

    public function setModel(){
        return UserInfoModel::class;
    }


    /**
     * 副管理列表
     * @param string $account
     */
    public function UserList(array $where,$post)
    {
        $arr =  $this->formatData(SystemAdminModel::where($where)->where('level',1)->paginate($post['limit']));

        return $arr;

    }


    //添加员工
    public function StaffSave($arr)
    {
        DB::beginTransaction();
        try{

            $systemAdminServices = app()->make(SystemAdminDao::class);
            $admin_id= $systemAdminServices->create($arr);
            $staff['admin_uid']=$admin_id;

           // $this->insert($staff);
            //插入日志
            $LogServices = app()->make(SystemLogServices::class);
            $LogServices->SaveLog($arr['creator_uid'],'添加员工','员工账号:'.$arr['account'].'--'.'员工名称:'.$arr['real_name']);

            DB::commit();
            return true;
        } catch(\Exception $exception) {
            DB::rollBack();
            throw new HOException($exception->getMessage());

        }
    }

    /**
     *  查看用户信息
     * */
    public function getStaff(array $where){
        /*$place = $this->models::select(['staff_name','phone','id','admin_uid'])
            ->with(['admininfo' => function($query)  {  //admininfo对应上面systemadmin模型中定义的admininfo方法名  闭包内是子查询
                return $query->select(['account','store_num','expiration_time','id','it_disabled']);
            }])->where($where)->first();
        $res = $this->formatData($place);
        $res['staff_id']=$where['id'];

        $res['admininfo']['user_id']=$res['admininfo']['id'];
        unset($res['id']);
        unset($res['admin_uid']);
        unset($res['admininfo']['id']);
        return $res;*/

         $data = $this->formatData(SystemAdminModel::where($where)->first());

         unset($data['pwd']);

        return $data;
    }


    /**
     * 编辑员工
     * */
    public function  StaffUpdate(string $staff_id,array $arr){

        $arr['expiration_time'] = SystemAdminModel::where('id',$staff_id)->value('expiration_time');
        if($arr['expiration_time'] == null){
            $arr['expiration_time'] = 0;
        }
        if($arr['expiration_time'] == '永久'){
            $arr['expiration_time'] = 0;
        }

        DB::beginTransaction();
        try{

            $systemAdminServices = app()->make(SystemAdminDao::class);

            //$creator_uid=$user['creator_uid'];
            //unset($user['creator_uid']);
            $admin_id= $systemAdminServices->update(['id'=>$staff_id],$arr);

            //$this->update(['id'=>$staff_id],$staff);
            //插入日志
            $LogServices = app()->make(SystemLogServices::class);
            $LogServices->SaveLog($arr['creator_uid'],'修改员工','员工id:'.$staff_id.'员工账号:'.$arr['account'].'--'.'员工名称:'.$arr['real_name']);

            DB::commit();
            return true;
        } catch(\Exception $exception) {
            DB::rollBack();
            throw new HOException($exception->getMessage());

        }
    }

    /*
     * 删除员工
     * */
    public function staffDelte($admin_id,$staff_id){

        DB::beginTransaction();
        try{


            $systemAdminServices = app()->make(SystemAdminDao::class);
            $systemAdminServices->update(['id'=>$staff_id],['deleted_at'=>date('Y-m-d H:i:s')]);

            //$this->update(['id'=>$staff_id],['deleted_at'=>date('Y-m-d H:i:s')]);
            //插入日志
            $LogServices = app()->make(SystemLogServices::class);
            $LogServices->SaveLog($admin_id,'删除代员工','员工id:'.$staff_id);

            DB::commit();
            return true;
        } catch(\Exception $exception) {
            DB::rollBack();
            throw new HOException($exception->getMessage());

        }
    }






}
